package com.jinke.uang.ui.activity.mine;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.MineModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.CalculateMoneyData;
import com.jinke.uang.ui.activity.personal.RepaymentActivity;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 展期界面
 */
@Route(name = "展期详情", path = RouterMap.ROLLOVER_PAGE)
public class DeferActivity extends BaseNormalActivity<CommonPresenter,MineModule>{


    @BindView(R.id.tvId)
    TextView tvId;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.tvMoney)
    TextView tvMoney;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvTotalFee)
    TextView tvTotalFee;
    @BindView(R.id.tvGoDefer)
    TextView tvGoDefer;
    private CalculateMoneyData mOrderItemData;
    private String mOrderId;
    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mOrderId = getIntent().getStringExtra(RepaymentActivity.ORDER_ID);
    }

    /**
     * 点击信息
     */
    private NoDoubleClickListener viewclick = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            super.onNoDoubleClick(v);
            switch (v.getId()) {
                case R.id.tvGoDefer:
                    //确认展期
                    rollover();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onUIReady() {
        tvGoDefer.setOnClickListener(viewclick);

        mPresenter.getData(HttpConstant.CALCULATED_ROLL_OVER_DATA,null,mOrderId);

    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_deferl;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.rollover_page_tile);
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected MineModule getModule() {
        return new MineModule();
    }

    @SuppressLint("SetTextI18n")
    private void fillUI(CalculateMoneyData data) {
        mOrderItemData = data;

        String moneyUnit = getResources().getString(R.string.money_unit);

        tvId.setText(getStringResource(R.string.borrow_detail_page_order_code) + " " + data
                .getOrderId());
        tvStatus.setText(getStringResource(R.string.repayment_type_overdue));
        tvMoney.setText(getStringResource(R.string.borrow_detail_page_loan_of_num) + ": " +
                moneyUnit + StringUtils.getDotMoneyStr(String.valueOf(data.getApplyAmt())));
        tvTotalFee.setText(getStringResource(R.string.rollover_total_fee_num) + ": " +
                moneyUnit + StringUtils.getDotMoneyStr(String.valueOf(data.getTotalFee())));
        tvTime.setText(getStringResource(R.string.borrow_detail_page_expiration_time) + ": " +
                data.getLastPayDate());
    }

    /**
     * 立即展期的按钮
     */
    public void rollover() {
        ARouter.getInstance()
                .build(RouterMap.REPAYMENT_PAGE)
                .withString(RepaymentActivity.ORDER_ID, mOrderItemData.getOrderId())
                .withString(RepaymentActivity.ORDER_MONEY_NUM, mOrderItemData.getTotalFee() + "")
                .withBoolean(RepaymentActivity.ORDER_MONEY_IS_ROLLOVER, true)
                .navigation();
    }



    @Override
    public void showResponse(int apiType, String listType, Object o) {
        if(apiType== HttpConstant.CALCULATED_ROLL_OVER_DATA){ //展期计算
            //点击展期进行提示
//            BaseResponse<CalculateMoneyData> response = (BaseResponse<CalculateMoneyData>) o;
            BaseResponse<String> response = (BaseResponse<String>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                //fillUI(response.getData());
            }else {
                ToastUtils.showShort(DeferActivity.this,response.getMessage());
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}
